package com.rui.controller;

import cn.hutool.core.util.ObjectUtil;
import com.rui.domain.*;
import com.rui.service.*;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@SuppressWarnings("all")
@RequestMapping("/teacher")
public class TeacherController {

    private static final String TEACHER = "teacher";
    private static final String YES_READ = "已阅";
    private static final String FLAG = "flag";

    @Autowired
    private StudentService studentService;
    @Autowired
    private InternshipService internshipService;
    @Autowired
    private DevelopmentLanguageService developmentLanguageService;
    @Autowired
    private DataBaseService dataBaseService;
    @Autowired
    private FrameService frameService;
    @Autowired
    private InternshipLoggerService internshipLoggerService;
    @Autowired
    private JobService jobService;
    @Autowired
    private TeacherService teacherService;

    @RequestMapping("/index")
    public String index() {
        return "teacher/index";
    }

    @RequestMapping("/toInputStudentInfo.action")
    public String toInputStudentInfo() {
        return "teacher/inputStudentInfo";
    }

    @RequestMapping("/toShowStudentInfo.action")
    public String toShowStudentInfo() {
        return "/teacher/showStudentInfo";
    }

    @RequestMapping("/addStudent.action")
    @ResponseBody
    public Map<String, Object> addStudent(Student student, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        //设置学生的系和指导老师
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        student.setTeacherId(teacher.getJobNo());
        //System.out.println(student);
        //设置用户信息
        User user = new User();
        user.setId(UUID.randomUUID().toString());
        user.setUsername(student.getStuNo());
        //加密初始密码123456
        Md5Hash password = new Md5Hash("123456", "encrypt", 3);
        user.setPassword(password.toString());
        user.setRoleId(3);
        user.setSalt("encrypt");
        //调用业务层保存学生信息和用户信息
        boolean b = studentService.saveStudentAndUser(student, user);
        if (b) {
            map.put("flag", true);
        } else {
            map.put("flag", false);
        }
        return map;
    }

    @RequestMapping("/getStudentInfo.action")
    @ResponseBody
    public Map<String, Object> getStudentInfo(HttpSession session, String year) {
        Map<String, Object> map = new HashMap<>();
        if (ObjectUtil.isEmpty(year)) {
            year = Calendar.getInstance().get(Calendar.YEAR) + "";
        }
        //先查询当前教师的学生信息
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        List<Student> students = studentService.findStudentsByJobNo(teacher.getJobNo(), Integer.valueOf(year));
        if (ObjectUtil.isEmpty(students)) {
            map.put("count", 0);
            map.put("data", new ArrayList<Student>());
        } else {
            map.put("count", students.size());
            map.put("data", students);
        }
        map.put("code", 0);
        map.put("msg", "没有查询到学生信息");
        return map;
    }

    @RequestMapping("/updateStudent.action")
    @ResponseBody
    public Map<String, Object> updateStudent(Student student, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        student.setTeacherId(teacher.getJobNo());
        System.out.println(student);
        boolean b = studentService.updateStudent(student);
        if (b) {
            map.put("flag", true);
        } else {
            map.put("flag", false);
        }
        return map;
    }

    @RequestMapping("/deleteStudent.action")
    @ResponseBody
    public Map<String, Object> deleteStudent(String stuNo) {
        Map<String, Object> map = new HashMap<>();
        //System.out.println(stuNo);
        boolean b = studentService.deleteStudent(stuNo);
        if (b) {
            map.put("flag", true);
        } else {
            map.put("flag", false);
        }
        return map;
    }

    @RequestMapping("/batchDelStudents.action")
    @ResponseBody
    public Map<String, Object> batchDelStudents(@RequestParam("arr[]") String[] arr) {
        Map<String, Object> map = new HashMap<>();
//        System.err.println(arr.length);
//        for (int i=0;i<arr.length;i++) {
//            System.out.println(arr[i]);
//        }
        boolean b = studentService.deleteBatchStudents(arr);
        if (b) {
            map.put("flag", true);
        } else {
            map.put("flag", false);
        }
        return map;
    }

    @GetMapping("/toShowStudentInternshipInfo.action")
    public String toShowStudentInternshipInfo() {
        return "teacher/showStudentInternshipInfo";
    }

    @RequestMapping("/getStudentInternshipInfo.action")
    @ResponseBody
    public Map<String, Object> getStudentInternshipInfo(HttpSession session, String year) {
        Map<String, Object> map = new HashMap<>();
        if (ObjectUtil.isEmpty(year)) {
            year = Calendar.getInstance().get(Calendar.YEAR) + "";
        }
        //查询当前教师所指导的学生的实习信息
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        List<InternshipVO> internshipVOList = internshipService.findInternshipByJobNo(teacher.getJobNo(), Integer.valueOf(year));
        //System.out.println(internshipVOList);
        if (ObjectUtil.isEmpty(internshipVOList)) {
            map.put("data", new ArrayList<InternshipVO>());
            map.put("count", 0);
        } else {
            map.put("data", internshipVOList);
            map.put("count", internshipVOList.size());
        }
        map.put("code", 0);
        map.put("msg", "没有查询到学生实习信息");
        return map;
    }

    @GetMapping("/toShowStudentInternshipLogInfo.action")
    public String toShowStudentInternshipLogInfo() {
        return "teacher/showStudentInternshipLogInfo";
    }

    @RequestMapping("/showStudentInternshipLogInfo.action")
    @ResponseBody
    public Map<String, Object> toShowStudentInternshipLogInfo(HttpSession session, String year) {
        Map<String, Object> map = new HashMap<>();
        if (ObjectUtil.isEmpty(year)) {
            year = Calendar.getInstance().get(Calendar.YEAR) + "";
        }
        //查询当前教师所指导的学生的实习信息
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        List<InternshipLogger> internshipLoggerList = internshipService.findInternshipLogByJobNo(teacher.getJobNo(), Integer.valueOf(year));
        map.put("data", internshipLoggerList);
        map.put("count", internshipLoggerList.size());
        map.put("code", 0);
        map.put("msg", "没有查询到学生实习日志信息");
        return map;
    }

    @RequestMapping("/checkInternshipLog.action")
    @ResponseBody
    public Map<String, Object> checkInternshipLog(InternshipLogger internshipLogger, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        //System.out.println(internshipLogger);
        Teacher teacher = (Teacher) session.getAttribute(TEACHER);
        internshipLogger.setTeacherName(teacher.getJobNo());
        internshipLogger.setUpdateTime(new Date());
        internshipLogger.setStatus(YES_READ);
        boolean b = internshipLoggerService.checkInternshipLog(internshipLogger);
        if (b) {
            map.put(FLAG, true);
        } else {
            map.put(FLAG, false);
        }
        return map;
    }

    @GetMapping("/toShowStudentJobInfo.action")
    public String toShowStudentJobInfo() {
        return "teacher/showStudentJobInfo";
    }

    @RequestMapping("/getStudentJobInfo.action")
    @ResponseBody
    public Map<String, Object> toShowStudentJobInfo(HttpSession session, String year) {
        Map<String, Object> map = new HashMap<>();
        if (ObjectUtil.isEmpty(year)) {
            year = Calendar.getInstance().get(Calendar.YEAR) + "";
        }
        //查询当前教师所指导的学生的就业信息
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        List<JobVO> jobVOList = jobService.findStudentJobByJobNo(teacher.getJobNo(), Integer.valueOf(year));
        map.put("data", jobVOList);
        map.put("count", jobVOList.size());
        map.put("code", 0);
        map.put("msg", "没有查询到学生实习日志信息");
        return map;
    }

    @RequestMapping("/getPersonInfo.action")
    @ResponseBody
    public Map<String, Object> getPersonInfo(HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        //System.out.println(academic);
        teacher = teacherService.findTeacherByJobNo(teacher.getJobNo());
        if (teacher == null) {
            return null;
        }
        map.put("teacher", teacher);
        return map;
    }

    @RequestMapping("/updatePerson.action")
    @ResponseBody
    public Map<String, Object> updatePerson(Teacher teacher) {
        Map<String, Object> map = new HashMap<>();
        boolean b = teacherService.updateTeacher(teacher);
        if (b) {
            map.put("flag", true);
        } else {
            map.put("flag", false);
        }
        return map;
    }
}
